import { showToast } from 'vant'
function getAccountForSorder(jsondata, idobject) {
console.log(idobject)
  // if (idobject === 'sorders' || idobject === 'sorders2') {
    let agent = ''
    let consumer = ''
    let type = ''
    let date = ''
    let sheng = ''

    for (let groupindex in jsondata.head) {
      for (let itemindex in jsondata.head[groupindex].groupvalue) {
        let item = jsondata.head[groupindex].groupvalue[itemindex]
        if (item.sField === 'agent') {
          agent = item.valuehidden
        }
        if (item.sField === 'consumer') {
          consumer = item.valuehidden
        }
        if (item.sField === 'type') {
          type = item.valuehidden
        }

        if (item.sField === 'date') {
          date = item.value
        }

        if (item.sField === 'sheng') {
          sheng = item.valuehidden
        }
      }
    }
    
    if (!date) {
      showToast('单据日期不能为空')
      return false
    }
    if (consumer === '') {
      showToast('请先选择客户')
      return false
    }
    // 自营
    if (type === '297ef43353a100de0153a1fdcaa60611') {
      if (agent !== '') {
        showToast('自营模式不能输入代理商')
        return false
      }
      return `${consumer};${date};${sheng}`
    } else {
      //非自营
      if (agent === '') {
        // showToast('请先选择代理商')
        // return false
        return `${consumer};${date};${sheng}`
      } else {
        return `${consumer};${agent};${date};${sheng}`
      }
    }
  // }
}
function getAgentForSorder2(jsondata, idobject) {
  // if (idobject === 'sorders') {
  let date = ''
  let tgxydh = ''
  let psxydh = ''

  for (let groupindex in jsondata.head) {
    for (let itemindex in jsondata.head[groupindex].groupvalue) {
      let item = jsondata.head[groupindex].groupvalue[itemindex]
      if (item.sField === 'date') {
        date = item.value
      }
    }
  }
  for (let groupindex in jsondata.bodydata) {
    for (let itemindex in jsondata.bodydata[groupindex]) {
      jsondata.bodydata[groupindex][itemindex].forEach((item) => {
        if (item.sField === 'tgxydh' && !tgxydh) {
          tgxydh = item.valuehidden
        }
        if (item.sField === 'psxydh' && !psxydh) {
          psxydh = item.valuehidden
        }
      })
    }
  }
  if (!date) {
    showToast('单据日期不能为空')
    return false
  }
  if (tgxydh) {
    return `${date};tgxy;${tgxydh}`
  }
  if (psxydh) {
    return `${date};psxy;${psxydh}`
  }
  return `${date}`
}
function getAccountForCzxfhsq(jsondata, idobject) {
  let date = ''
  let khmc = ''

  for (let groupindex in jsondata.head) {
    for (let itemindex in jsondata.head[groupindex].groupvalue) {
      let item = jsondata.head[groupindex].groupvalue[itemindex]
      if (item.sField === 'date') {
        date = item.value
      }
      if (item.sField === 'khmc') {
        khmc = item.valuehidden
      }
    }
  }
  if (!date) {
    showToast('单据日期不能为空')
    return false
  }
  if (!khmc) {
    showToast('请先输入客户')
    return false
  }
  return `${khmc};${date}`
}

function getFeebillFyzl(jsondata, idobject) {
  if (idobject === 'fee') {
    //  // 费用报销单表体费用类别
    let fyzl = ''
    for (let groupindex in jsondata.head) {
      for (let itemindex in jsondata.head[groupindex].groupvalue) {
        let item = jsondata.head[groupindex].groupvalue[itemindex]
        if (item.sField === 'fyzl') {
          fyzl = item.valuehidden
        }
      }
    }
    if (!fyzl) {
      showToast('请先选择费用种类')
      return
    }
    let json = {
      feebill_fyzl: fyzl
    }
    // console.log(JSON.stringify(json))
    return base64encode(JSON.stringify(json))
  }
}

function chenxinHzxdlProduct(jsondata, idobject) {
  if (idobject === 'dlprice') {
    // 辰欣项目代码
    refparams = []
    let iddep = ''
    let dlkh = ''
    for (let groupindex in jsondata.head) {
      for (let itemindex in jsondata.head[groupindex].groupvalue) {
        let item = jsondata.head[groupindex].groupvalue[itemindex]
        if (item.sField === 'iddep') {
          iddep = item.valuehidden
        }
        if (item.sField == 'dlkh') {
          dlkh = item.valuehidden
        }
      }
    }
    if (!dlkh) {
      showToast('请先选择联系人')
      return false
    }
    if (!iddep) {
      showToast('请先选择部门')
      return false
    }
    return (refparams = iddep + ';' + dlkh)
  }
}

function getAccountForSorderMkw(jsondata, idobject) {
  if (/^sorders\d?$/.test(idobject)) {
    let consumer = ''
    let sheng = ''
    let date = ''
    let country = ''

    const fieldMap = {
      consumer: (value) => (consumer = value),
      date: (value) => (date = value),
      sheng: (value) => (sheng = value),
      country: (value) => (country = value)
    }

    jsondata.head.forEach((group) => {
      group.groupvalue.forEach((item) => {
        const handler = fieldMap[item.sField]
        if (handler) {
          handler(item.valuehidden || item.value)
        }
      })
    })

    sheng = sheng.length ? sheng : ''

    if (!date) {
      showToast('单据日期不能为空')
      return false
    }

    if (consumer) {
      refparams = `${consumer};${date};${sheng};${country}`
    } else {
      showToast('请先输入客户')
      return false
    }
  }
}
// 迈科维，销售订单 经销商插入点 (参照PC端逻辑)
function getDzForSorder(jsondata, idobject) {
  if (/^sorder\d?$/.test(idobject)) {
    let date = ''
    let paycompany = ''
    let tgxydh = ''
    let psxydh = ''

    const fieldMap = {
      date: (value) => (date = value),
      paycompany: (value) => (paycompany = value),
      tgxydh: (value) => (tgxydh = value),
      psxydh: (value) => (psxydh = value)
    }

    jsondata.head.forEach((group) => {
      group.groupvalue.forEach((item) => {
        const handler = fieldMap[item.sField]
        if (handler) {
          handler(item.valuehidden || item.value)
        }
      })
    })

    if (!date) {
      showToast('单据日期不能为空')
      return false
    }
    if (!paycompany) {
      showToast('发货主体不能为空!')
      return false
    }

    refparams = `${date};`
    if (tgxydh) {
      refparams += `tgxy;${tgxydh};`
    }
    if (psxydh) {
      refparams += `psxy;${psxydh}; `
    }
    refparams += `${paycompany}`

    return refparams
  }
}

function getDzForSorder3(jsondata, idobject) {
  getDzForSorder(jsondata, idobject)
}

function chenxinXhjhProduct(jsondata, idobject) {
  if (idobject === 'xhjhdetail') {
    // 辰欣项目代码
    let iddep = ''
    for (let groupindex in jsondata.head) {
      for (let itemindex in jsondata.head[groupindex].groupvalue) {
        let item = jsondata.head[groupindex].groupvalue[itemindex]
        if (item.sField === 'iddep') {
          iddep = item.valuehidden
        }
      }
    }
    if (!iddep) {
      showToast('请检查部门是否为空!')
      return
    }
    return iddep
  }
}

var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
var base64DecodeChars = new Array(
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  62,
  -1,
  -1,
  -1,
  63,
  52,
  53,
  54,
  55,
  56,
  57,
  58,
  59,
  60,
  61,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  0,
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
  11,
  12,
  13,
  14,
  15,
  16,
  17,
  18,
  19,
  20,
  21,
  22,
  23,
  24,
  25,
  -1,
  -1,
  -1,
  -1,
  -1,
  -1,
  26,
  27,
  28,
  29,
  30,
  31,
  32,
  33,
  34,
  35,
  36,
  37,
  38,
  39,
  40,
  41,
  42,
  43,
  44,
  45,
  46,
  47,
  48,
  49,
  50,
  51,
  -1,
  -1,
  -1,
  -1,
  -1
)
// client Base64 encode
function base64encode(str) {
  var out, i, len
  var c1, c2, c3
  var strURI
  strURI = encodeURIComponent(str)
  i = 0
  out = ''
  len = strURI.length
  while (i < len) {
    c1 = strURI.charCodeAt(i++) & 255
    if (i == len) {
      out += base64EncodeChars.charAt(c1 >> 2)
      out += base64EncodeChars.charAt((c1 & 3) << 4)
      out += '=='
      break
    }
    c2 = strURI.charCodeAt(i++)
    if (i == len) {
      out += base64EncodeChars.charAt(c1 >> 2)
      out += base64EncodeChars.charAt(((c1 & 3) << 4) | ((c2 & 240) >> 4))
      out += base64EncodeChars.charAt((c2 & 15) << 2)
      out += '='
      break
    }
    c3 = strURI.charCodeAt(i++)
    out += base64EncodeChars.charAt(c1 >> 2)
    out += base64EncodeChars.charAt(((c1 & 3) << 4) | ((c2 & 240) >> 4))
    out += base64EncodeChars.charAt(((c2 & 15) << 2) | ((c3 & 192) >> 6))
    out += base64EncodeChars.charAt(c3 & 63)
  }
  return out
}

function getProductForPsxy(jsondata, idobject) {
  if (idobject === 'psxys') {
    //  // 费用报销单表体费用类别
    let qy = '' //省
    let shi = ''
    let sdate = ''
    let czms = ''
    for (let groupindex in jsondata.head) {
      for (let itemindex in jsondata.head[groupindex].groupvalue) {
        let item = jsondata.head[groupindex].groupvalue[itemindex]
        if (item.sField === 'qy') {
          qy = item.valuehidden
        }
        if (item.sField === 'shi') {
          shi = item.valuehidden
        }
        if (item.sField === 'sdate') {
          sdate = item.value
        }
        if (item.sField === 'czms') {
          czms = item.valuehidden
        }
      }
    }
    if (!qy) {
      showToast('省份不能为空')
      return
    }
    if (!sdate) {
      showToast('生效日期不能为空')
      return
    }
    if (!czms) {
      showToast('操作模式不能为空')
      return
    }

    let res = ''
    if (shi) {
      res = `${qy};${shi};${sdate};${czms}`
    } else {
      res = `${qy};${sdate};${czms}`
    }
    return res
  }
}

export default {
  getAccountForSorder,
  getAgentForSorder2,
  chenxinHzxdlProduct,
  chenxinXhjhProduct,
  getDzForSorder3,
  getDzForSorder,
  getAccountForSorderMkw,
  getFeebillFyzl,
  getProductForPsxy,
  getAccountForCzxfhsq
}
